﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.Model;
using rock.DAL;
using System.Threading;
using rock.common;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;

 


namespace rock.UI  
{
    
    public partial class Login : Form
    {

        

        private DalUSER_ACCOUNT bll = new DalUSER_ACCOUNT();
        private DalUSER_AUTHO bllAutho = new DalUSER_AUTHO();
        public Login()
        {
            InitializeComponent();
            this.Icon = global::rock.Properties.Resources.house;
            Manager.getInstance().skinEngine = new Sunisoft.IrisSkin.SkinEngine();
            //初使皮肤
            String skinFile = ConfigUtils.GetAppConfig("Manager.getInstance().skinEngine.SkinStream");
            if (skinFile != null && skinFile.EndsWith(".ssk"))
            {
                setSkin("DiamondBlue.ssk");
            }
            else
            {
                setSkin("DiamondBlue.ssk");
            }


            String lastStartupDate = ConfigUtils.GetAppConfig("database");

            String setupDate = ConfigUtils.GetAppConfig("database2");
        }




        private delegate void DelegateLoadUser();
        private DelegateLoadUser delegateLoadUser = null;//   new DelegateLoadUser(MyThreadWork);

        private void Login_Load(object sender, EventArgs e)
        {

            delegateLoadUser = new DelegateLoadUser(loadUser);
            ThreadPool.QueueUserWorkItem(state =>{
                this.Invoke(delegateLoadUser);
            });
        }
  
        private void loadUser()
        {
            try
            {

                DataSet ds = bll.GetListForLogin("");
                this.account_comboBox.DisplayMember = "USERNAME";
                this.account_comboBox.ValueMember = "ACCOUNT";
                this.account_comboBox.DataSource = ds.Tables[0];
            }
            catch (Exception)
            {

                MessageBox.Show( "数据库连接失败,请配置数据库参数.", "提示");
                FormConfigSql form = new FormConfigSql();
                form.oneRefreshParentData = refreshParentData;
                form.ShowDialog();
            }
            
        }
 
 

        private void refreshParentData()
        {
            loadUser();
        }

         
        //登陆
        private void button1_Click(object sender, EventArgs e)
        {
            String account =(String) this.account_comboBox.SelectedValue;
            if (account == null || account.Equals("")) {
                account = this.account_comboBox.Text;
            }
            String password = this.password_textBox1.Text;


            ModelUSER_ACCOUNT user = null;
            try
            {
                user = bll.GetModel(account);
            }
            catch (Exception)
            {

                MessageBox.Show("数据库连接失败,请配置数据库参数.","提示");
                FormConfigSql form = new FormConfigSql();
               form.ShowDialog(this);
            }
          


            if (user == null) 
            {
                MessageBox.Show("请选择正确的用户姓名或输入用户登陆帐号", "提示");
                return;
            }
            else if(password==null || password.Equals(""))
            {
                MessageBox.Show("登陆密码不能为空!", "提示");
                return;
            }
            else if (!user.PASSWORD.Equals(password))
            {
                MessageBox.Show("请选择输入正确的登陆密码", "提示");
                return;
            }
            //登陆成功
            Manager.getInstance().userAccount = user;
            Manager.getInstance().loginFrom = this;
            ModelUSER_AUTHO autho = bllAutho.GetModel(account);
            Manager.getInstance().userAutho = autho;

            int authoValue =
                autho.BASE_AUTHO +
                autho.FEE_AUTHO +
                autho.REPORT_AUTHO +
                autho.HOUSE_AUTHO +
                autho.CONFIG_AUTHO +
                autho.MESSAGE_AUTHO +
                autho.PICTURE_AUTHO;

            if (authoValue == 0) {
                MessageBox.Show("此用户还未分配任何权限,如需登陆.请联系系统管理员.", "提示");
                return; 
            }

           
            this.Visible = false;
            Main mainFrom = new Main();
            mainFrom.Login = this; 
            mainFrom.Show();
        }

        
        

        
        //退出
        private void button2_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(System.Environment.ExitCode);


           
        }

         

        private void button3_Click(object sender, EventArgs e)
        {

        }

        

        private void Login_Activated(object sender, EventArgs e)
        {
            
        }



        private void setSkin(String skinFile)
        {

            Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondBlue);
            Manager.getInstance().skinEngine.Active = true;
            
            //if (skinFile.Equals("Defalult.ssk"))
            //{
            //    Manager.getInstance().skinEngine.SkinFile = null;
            //    Manager.getInstance().skinEngine.Active = false;
            //    ConfigUtils.UpdateAppConfig("Manager.getInstance().skinEngine.SkinStream", skinFile);
            //    return;
            //}
            //else if (skinFile.Equals("DiamondGreen.ssk"))
            //{
            //     Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondGreen);
               
            //}
            //else if (skinFile.Equals("DiamondOlive.ssk"))
            //{
            //     Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondOlive);
             
            //}
            //else if (skinFile.Equals("DiamondPurple.ssk"))
            //{
            //     Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondPurple);
              
            //}
            //else if (skinFile.Equals("DiamondBlue.ssk"))
            //{
            //     Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondBlue);
                
            //}
            //else if (skinFile.Equals("DiamondRed.ssk"))
            //{
            //     Manager.getInstance().skinEngine.SkinStream = new MemoryStream(global::rock.Properties.Resources.DiamondRed);
                
            //}
           
           // ConfigUtils.UpdateAppConfig("Manager.getInstance().skinEngine.SkinStream", skinFile);

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

         
    }

    
    
}
